$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/takeoutset/getTakeOut',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
            { label: '店铺名称', name: 'shelvesname', index: 'shelvesname', width: 70},
            { label: '店铺地址', name: 'shelvesaddress', index: 'shelvesaddress', width: 150},
            { label: '货架分组', name: 'shelvesgroup', index: 'shelvesgroup', width: 50,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.shelvesgroup!=null&&rowdata.shelvesgroup!=""){
                        var groupname = "";
                        var arr = rowdata.shelvesgroup.split(",");
                        for(var i=0;i<arr.length;i++){
                            $.ajax({
                                type: "POST",
                                async:false,
                                url: baseURL + "sys/shelvesgroup/info/"+parseInt(arr[i]),
                                success: function(r){
                                    groupname +=r.shelvesgroup.groupname;
                                    if(i !=arr.length-1){
                                        groupname +="/";
                                    }

                                }
                            });
                        }
                        return groupname;
                    }else{
                        return '未分组';
                    }


                }},
            { label: '状态', name: 'statu', index: 'statu', width: 50,formatter: function(item, index) {
                    if (item === 0) {
                        return '启用';
                    }
                    if (item === 1) {
                        return '禁用';
                    }
                } },
            { label: '创建时间', name: 'createtime', index: 'createtime', width: 80 },
            { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' onclick='vm.update("+ rowdata.id +")'>" +
                        "编辑</a>&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' onclick='vm.del("+ rowdata.id +")'>" +
                        "删除</a>";
                }}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

});

var vm = new Vue({
	el:'#rrapp',
	data:{
	    q:{
	       name:null,
           statu:null
        },
		showList: true,
		title: null,
		takeoutset: {},
        times:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.takeoutset = {};
            vm.takeoutset.statu = 0;
            vm.takeoutset.autoreceipt = 0;
            vm.queryGoodshelves();
            vm.getProvince();
            vm.getCity();
            vm.getArea();
            $("#yysj").html("");
            var temp = "<div style='width:390px;height:35px;margin-bottom:3px;'>" +
                "<div class='col-md-4' style='display: -webkit-inline-box;padding-left:0px'>" +
                "<div class='input-group'><input type='text' id='startTime' name='startTime' " +
                "readonly='readonly' class='form-control timepicker timepicker-default' placeholder='开始时间'>" +
                "<span class='input-group-btn'><button class='btn default' type='button'>" +
                "<i class='fa fa-clock-o'></i></button></span></div></div><div style='padding-left: 0px;float:left;padding-top:8px;'>" +
                "至</div><div class='col-md-4'><div class='input-group'>" +
                "<input type='text' id='endTime' name='endTime' readonly='readonly' class='form-control timepicker timepicker-default' placeholder='结束时间'>" +
                "<span class='input-group-btn'><button class='btn default' type='button'><i class='fa fa-clock-o'></i>" +
                "</button></span></div></div></div>";
            $("#yysj").append(temp);
            $('.timepicker').timepicker({
                showMeridian:false
            });
		},
		update: function (id) {

			vm.showList = false;
            vm.title = "修改";

            vm.times = [];
            vm.getInfo(id);
            vm.getProvince();
            vm.getCity();
            vm.getArea();
            vm.queryGoodshelves();
            var times = vm.takeoutset.dailytime;
            $("#yysj").html("");
            var timeArray = times.split(",");
            var temp = "";
            for(var i=0;i<timeArray.length;i++){
                var timeArrayOne = timeArray[i].split("-");

                temp += "<div style='width:390px;height:35px;margin-bottom:3px;' id='sj"+(i+1)+"'><div class='col-md-4' style='display: -webkit-inline-box;padding-left:0px'>" +
                    "<div class='input-group'><input type='text' id='startTime' name='startTime' readonly='readonly' " +
                    "class='form-control timepicker timepicker-default' placeholder='从' value='"+timeArrayOne[0]+"'><span class='input-group-btn'>" +
                    "<button class='btn default' type='button'><i class='fa fa-clock-o'></i></button></span></div></div>" +
                    "<div style='padding-left: 0px;float:left;padding-top:8px;'>至</div><div class='col-md-4'>" +
                    "<div class='input-group'><input type='text' id='endTime' name='endTime' readonly='readonly' " +
                    "class='form-control timepicker timepicker-default' placeholder='到' value='"+timeArrayOne[1]+"'><span class='input-group-btn'>" +
                    "<button class='btn default' type='button'><i class='fa fa-clock-o'></i></button></span></div></div>" +
                    "<div style='float: left;padding-left: 3px;padding-top:8px;'>";
                    if(i!=0){
                        temp +="<a><img style='cursor: pointer' src='../../statics/img/ico16-delete.png' width='16' height='16' onclick=vm.removeBusTime('sj"+(i+1)+"')></a>";
                    }

                    temp +="</div></div>";

                var obj = {};
                obj.id = timeArray.length+1;
                vm.times.push(obj);
            }
            $("#yysj").append(temp);
            $('.timepicker').timepicker({
                showMeridian:false
            });

		},
		setTakeOut: function () {
            if(vm.takeoutset.statu == 0){
                $("#wm").show();
            }
            if(vm.takeoutset.statu == 1){
                $("#wm").hide();
            }
        },
		saveOrUpdate: function (event) {
			var url = vm.takeoutset.id == null ? "sys/takeoutset/save" : "sys/takeoutset/update";
            vm.takeoutset.detailedaddress = $("#detailedaddress").val();
            var startArr = [];
            var endArr = [];

            vm.takeoutset.dailytime = "";
            $("input[name='startTime']").each(function(){
                startArr.push($(this).val());
            });
            $("input[name='endTime']").each(function(){
                endArr.push($(this).val());
            });
            for(var i=0;i<startArr.length;i++){
                console.log(startArr[i])
                vm.takeoutset.dailytime +=startArr[i]+"-"+endArr[i];
                if(i!=startArr.length-1){
                    vm.takeoutset.dailytime +=",";
                }
            }
            if(vm.takeoutset.telphone == '' || typeof vm.takeoutset.telphone=='undefined'){
                alert("联系电话不能为空");
            }else if(vm.takeoutset.detailedaddress == '' || typeof vm.takeoutset.detailedaddress=='undefined'){
                alert("详细地址不能为空");
            }else if(vm.takeoutset.ranges == '' || typeof vm.takeoutset.ranges=='undefined'){
                alert("配送范围不能为空");
            }else if(vm.takeoutset.sendprice == '' || typeof vm.takeoutset.sendprice=='undefined'){
                alert("起送价格不能为空");
            }else if(vm.takeoutset.fee == '' || typeof vm.takeoutset.fee=='undefined'){
                alert("配送费不能为空");
            }else{
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.takeoutset),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }

		},
		del: function (id) {
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/takeoutset/delete/"+id,
                    contentType: "application/json",
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        queryGoodshelves: function(){
            $("#sheelves_id").html("");
            $.get(baseURL + "sys/goodshelves/alllist", function(r){
                var temp = "";
                $.each(r.alllist, function (i) {
                    if(vm.takeoutset.shelvesid ==''|| typeof vm.takeoutset.shelvesid=='undefined'){
                        if(i ==0){
                            vm.takeoutset.shelvesid = r.alllist[i].id;
                        }

                    }
                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].shelvesname+"</option>";

                });
                $("#sheelves_id").append(temp);

            });
        },
		getInfo: function(id){
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/takeoutset/info/"+id,
                success: function(r){
                    vm.takeoutset = r.takeoutset;

                }
            });
		},
        getProvince: function(){
            $("#province").html("");
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/hatprovince/alllist",
                success: function(r){
                    var temp = "";
                    $.each(r.alllist, function (i) {
                        if(vm.takeoutset.province ==''|| typeof vm.takeoutset.province=='undefined'){
                            if(i ==0){
                                vm.takeoutset.province = r.alllist[i].provinceid;
                            }

                        }
                        temp +="<option value='"+r.alllist[i].provinceid+"'>"+r.alllist[i].province+"</option>";

                    });
                    $("#province").append(temp);

                }
            });

        },
        getCity: function(){
            $("#city").html("");
            var id = vm.takeoutset.province;
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/hatcity/alllist/"+id,
                success: function(r){
                    var temp = "";
                    $.each(r.alllist, function (i) {
                        if(vm.takeoutset.city ==''|| typeof vm.takeoutset.city=='undefined'){
                            if(i ==0){
                                vm.takeoutset.city = r.alllist[i].cityid;
                            }

                        }

                        temp +="<option value='"+r.alllist[i].cityid+"'>"+r.alllist[i].city+"</option>";

                    });
                    $("#city").append(temp);
                    vm.getArea();

                }
            });


        },
        getArea: function(){
            $("#area").html("");
            var id = $("#city").val();
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/hatarea/alllist/"+id,
                success: function(r){
                    var temp = "";
                    $.each(r.alllist, function (i) {
                        if(vm.takeoutset.area ==''|| typeof vm.takeoutset.area=='undefined'){
                            if(i ==0){
                                vm.takeoutset.area = r.alllist[i].areaid;
                            }

                        }
                        temp +="<option value='"+r.alllist[i].areaid+"'>"+r.alllist[i].area+"</option>";

                    });
                    $("#area").append(temp);
                }
            });
        },
        addBusTime: function () {
		    var temp = "";
		    if(vm.times.length>0){

                temp = "<div style='width:390px;height:35px;margin-bottom:3px;' id='sj"+(vm.times.length+1)+"'><div class='col-md-4' style='display: -webkit-inline-box;padding-left:0px'>" +
                    "<div class='input-group'><input type='text' id='startTime' name='startTime' readonly='readonly' " +
                    "class='form-control timepicker timepicker-default' placeholder='从'><span class='input-group-btn'>" +
                    "<button class='btn default' type='button'><i class='fa fa-clock-o'></i></button></span></div></div>" +
                    "<div style='padding-left: 0px;float:left;padding-top:8px;'>至</div><div class='col-md-4'>" +
                    "<div class='input-group'><input type='text' id='endTime' name='endTime' readonly='readonly' " +
                    "class='form-control timepicker timepicker-default' placeholder='到'><span class='input-group-btn'>" +
                    "<button class='btn default' type='button'><i class='fa fa-clock-o'></i></button></span></div></div>" +
                    "<div style='float: left;padding-left: 3px;padding-top:8px;'><a>" +
                    "<img style='cursor: pointer' src='../../statics/img/ico16-delete.png' width='16' height='16' onclick=vm.removeBusTime('sj"+(vm.times.length+1)+"')></a></div></div>";


            }else{
                temp = "<div style='width:390px;height:35px;margin-bottom:3px;' id='sj1'><div class='col-md-4' style='display: -webkit-inline-box;padding-left:0px'>" +
                    "<div class='input-group'><input type='text' id='startTime' name='startTime' readonly='readonly' " +
                    "class='form-control timepicker timepicker-default' placeholder='从'><span class='input-group-btn'>" +
                    "<button class='btn default' type='button'><i class='fa fa-clock-o'></i></button></span></div></div>" +
                    "<div style='padding-left: 0px;float:left;padding-top:8px;'>至</div><div class='col-md-4'>" +
                    "<div class='input-group'><input type='text' id='endTime' name='endTime' readonly='readonly' " +
                    "class='form-control timepicker timepicker-default' placeholder='到'><span class='input-group-btn'>" +
                    "<button class='btn default' type='button'><i class='fa fa-clock-o'></i></button></span></div></div>" +
                    "<div style='float: left;padding-left: 3px;padding-top:8px;'><a>" +
                    "<img style='cursor: pointer' src='../../statics/img/ico16-delete.png' width='16' height='16' onclick=vm.removeBusTime('sj1')></a></div></div>";
                var obj = {};
                obj.id = 1;
                vm.times.push(obj);
            }

            $("#yysj").append(temp);
            $('.timepicker').timepicker({
                showMeridian:false
            });
        },
        removeBusTime: function (id) {
            $("#"+id).remove();
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.name,statu:vm.q.statu},
                page:page
            }).trigger("reloadGrid");
		}
	}
});